package g20;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class Arbre implements Comparator<Node> {
	
	private SortedSet<Node> stack = new TreeSet<Node>(this);
	private Set<Node> visited = new HashSet<Node>();
	private Node root;
	
	Arbre(Taquin elt) {
		root = new Node(elt, null);
		stack.add(root);
	}
	
	Node deployAStar() {
		while (!stack.isEmpty()) {
			Node n = stack.first();
			visited.add(n);
			stack.remove(n);
			if (n.elt.getCoutManhattanMinusParent() == 0)
				return n;
			List<Node> l = n.deployAStar();
			for (Node node : l)
				if (!visited.contains(node))
					stack.add(node);
		}
		return null;
	}
	
	public int compare(Node o1, Node o2) {
		int res = o1.elt.getCoutManhattan() - o2.elt.getCoutManhattan();
		if (res == 0)
			return o1.compareTo(o2);
		else if (res > 0)
			if (o1.compareTo(o2) == 0)
				return 0;
		return res;
	}
}
